package com.inspectortime.webapp.inspector.fee;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.pricing.BaseFeeWithAdjustersCalculator;
import com.inspectortime.domain.pricing.FeeCalculator;
import com.inspectortime.domain.pricing.PricingPlanType;

public class FeeWizardListingPriceHelper extends FeeWizardCommonHelper {

	private class ListEntry {
		public String field;
		public String label;
		public float maxListingPrice;
		public float minListingPrice;
		public double price;

		public boolean equals(ListEntry o) {
			return o.field.equals(this.field);
		}

		public String toString() {
			return field + ": " + minListingPrice + " min, " + maxListingPrice
					+ " max";
		}
	}

	private static final String[] listPriceRanges = { "0-50", "50-100",
			"100-150", "150-200", "200-250", "250-300", "300-350", "350-400", "400-500",
			"500-600", "600-700", "700-800", "800-1000", "1000-1200",
			"1200-1400", "1400-2000", "2000-3000", "3000-10000" };

	private List<ListEntry> entries = new ArrayList<ListEntry>();

	public FeeWizardListingPriceHelper(double defaultPrice) {

		this.setType(PricingPlanType.ListingPrice);

		// Initial list price ranges
		ListEntry entry = null;
		for (int i = 0; i < listPriceRanges.length; i++) {
			entry = new ListEntry();
			entry.field = listPriceRanges[i];
			entry.label = this.getListPriceRangeLabel(listPriceRanges[i]);
			entry.minListingPrice = this.getRangeStart(listPriceRanges[i]);
			entry.maxListingPrice = this.getRangeEnd(listPriceRanges[i]);
			entry.price = defaultPrice;
			entries.add(entry);
		}
	}

	@Override
	public List<BaseFeeWithAdjustersCalculator> createFeeCalculators() {
		List<BaseFeeWithAdjustersCalculator> calculators = new ArrayList<BaseFeeWithAdjustersCalculator>();
		BaseFeeWithAdjustersCalculator calculator = null;
		for (ListEntry entry : entries) {
			calculator = new BaseFeeWithAdjustersCalculator();
			calculators.add(calculator);
			calculator.setBaseFee(entry.price);
			calculator.setMinListedPrice(entry.minListingPrice);
			calculator.setMaxListedPrice(entry.maxListingPrice);
			calculator.setSource(entry.field);
		}
		return calculators;
	}

	private ListEntry getEntry(String field) {
		for (ListEntry entry : entries) {
			if (entry.field.equals(field)) {
				return entry;
			}
		}
		return null;
	}

	public Map<String, Double> getListPriceRangeFees() {
		Map<String, Double> result = new LinkedHashMap<String, Double>();
		for (ListEntry entry : entries) {
			result.put(entry.field, entry.price);
		}
		return result;
	}

	public String getListPriceRangeLabel(String listPriceRange) {
		String[] vals = listPriceRange.split("-");
		int start = Integer.valueOf(vals[0]);
		int end = Integer.valueOf(vals[1]);
		start = start * 1000;
		end = (end * 1000) - 1;
		DecimalFormat fmt = new DecimalFormat("###,###,##0");
		String label = fmt.format(start) + " - " + fmt.format(end);
		return label;
	}

	private float getRangeEnd(String range) {
		String[] vals = range.split("-");
		float end = Integer.valueOf(vals[1]);
		end = (end * 1000) - 0.01f;
		return end;
	}
	
	private float getRangeStart(String range) {
		String[] vals = range.split("-");
		int start = Integer.valueOf(vals[0]);
		start = start * 1000;
		return start;
	}
	
	public void populateFromCalculators(InspectorCompany company) {
		populateFromCompany(company);
		for (FeeCalculator calculator : company.getFeeCalculators()) {
			if (calculator.getSource() == null) {
				continue;
			}
			ListEntry entry = this.getEntry(calculator.getSource());
			if (entry == null) {
				continue;
			}
			entry.label = this.getListPriceRangeLabel(calculator.getSource());
			entry.minListingPrice = calculator.getMinListedPrice();
			entry.maxListingPrice = calculator.getMaxListedPrice();
			entry.price = calculator.getBaseFee();
		}
	}
	
	public void setPrice(String field, double amount) {
		ListEntry entry = getEntry(field);
		entry.price = amount;
	}
	
	public void sortEntries() {
		Collections.sort(entries, new Comparator() {
			public int compare(Object o1, Object o2) {
				ListEntry e1 = (ListEntry) o1;
				ListEntry e2 = (ListEntry) o2;
				return (int) (e1.minListingPrice - e2.minListingPrice);
			}
		});
	}


}
